home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / DHEAP1.C < prev    next >
C/C++ Source or Header  |  1993-05-05  |  2KB  |  80 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 05-03-93 (21:32)             Number: 48
  4. From: RAY GARDNER                  Refer#: NONE
  5.   To: LES FENISON                   Recvd: NO  
  6. Subj: Determining heap memory l      Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.  > Does anyone have any routines to determine how much memory is actually
  9.  > available on the heap including freed memory located before the last
  10.  > allocated?
  11.  
  12. For use in large or compact model, where there is no separate far heap (i.e. the
  13.  only heap is the far heap):
  14.  
  15. /* memleft() -- find remaining available memory
  16. **  public domain by Ray Gardner 4/93
  17. **
  18. **  Allocate (then free) all remaining memory to determine
  19. **  how much is left.  Set allocated bytes to val.  (This is valuable
  20. **  for debugging, when val turns up where it doesn't belong.)
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. typedef struct NODEtag {
  26.     struct NODEtag *next;
  27.     } NODE;
  28.  
  29. long memleft(unsigned val)
  30. {
  31.     long total = 0;
  32.     unsigned k;
  33.     NODE *p, *list = 0;
  34.  
  35.     for ( k = 32768U; k >= sizeof(NODE *); )
  36.     {
  37.         if ( (p = (NODE *)malloc(k)) == 0 ) {
  38.             k /= 2;
  39.         }
  40.         else
  41.         {
  42.             total += k;
  43.             memset((void*)p, val, k);
  44.             p->next = list;
  45.             list = p;
  46.         }
  47.     }
  48.  
  49.     while ( list )
  50.     {
  51.         p = list->next;
  52.         memset((void *)list, val, sizeof(NODE *));
  53.         free((char *)list);
  54.         list = p;
  55.     }
  56.  
  57.     return total;
  58. }
  59.  
  60. #ifdef TEST
  61. #include <stdio.h>
  62. int main(void)
  63. {
  64.     printf("%ld\n", memleft(0xCF));
  65.     return 0;
  66. }
  67. #endif
  68.  
  69. I have a similar function for use in checking the far heap from a small model pr
  70. ogram.  I can post it if you need it, or perhaps you'd like to try adapting this
  71.  one.
  72.  
  73.  
  74.  
  75. --- msged 1.99S ZTC
  76.  * Origin: Ray Gardner -- Englewood, CO (1:104/89.2)
  77. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  78. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  79. SEEN-BY: 390/1 396/1 5 15 2270/1 2440/5 3603/20
  80.